package com.aistar.controller;

import com.aistar.mapper.AncommMapper;
import com.aistar.pojo.*;
import com.aistar.service.AncommService;
import com.aistar.service.AnswerService;
import com.aistar.service.MemberService;
import com.aistar.service.QuestionService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.junit.runners.Parameterized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Pattern;
import java.util.*;

@Controller
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private AncommService ancommService;
    @Autowired
    private AncommMapper ancommMapper;

    @GetMapping("QuestionTable/club_questions")
    public ModelAndView getall(HttpServletRequest request){
        /* 根据评论数量进行降序排列 */
        //1. 获得url后面页码
        List<QuestionMember> questionMember=new ArrayList<>();
        String pageNumStr = request.getParameter("page");
        System.out.println("pageNumStr:"+pageNumStr);
        Integer pageNum = null;
        //2. 页码数据类型转换
        if(pageNumStr ==null)
            pageNum = 1;
        else
            pageNum = Integer.valueOf(pageNumStr);

        //3. service 调用

        PageInfo<Question> pageInfo = questionService.getAll(pageNum,3);
        System.out.println("pageInfo =" + pageInfo.getList());
        //数据绑定

        List<Question> questionList = pageInfo.getList();

        for(Question que:questionList){
            Member member = memberService.getbyId(que.getQueId());//查到对应游记的用户信息
            questionMember.add(new QuestionMember(que,member));
        }


     /*   根据发布时间来进行排序 */

        //1. 获得url后面页码
        List<QuestionMember> questionMemberdate=new ArrayList<>();
        String pageNumStrdate = request.getParameter("datepage");
        System.out.println(" pageNumStrdate:"+pageNumStrdate);
        Integer pageNumdate = null;
        //2. 页码数据类型转换
        if(pageNumStrdate ==null)
            pageNumdate = 1;
        else
            pageNumdate= Integer.valueOf(pageNumStrdate);

        //3. service 调用

        PageInfo<Question> pageInfodate = questionService.getAllfromdate(pageNumdate,3);
        System.out.println("pageInfodate =" + pageInfo.getList());
        //数据绑定

        List<Question> questionListdate = pageInfodate.getList();

        for(Question que:questionListdate){
            Member memberdate = memberService.getbyId(que.getQueId());//查到对应游记的用户信息
            questionMemberdate.add(new QuestionMember(que,memberdate));
        }

  /************************************************************************************/
        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        Member member = memberService.getbyId(userId);
  /*************************************************************************/
        ModelAndView mav = new ModelAndView();

        mav.addObject("questionList",questionMember);
        mav.addObject("page",pageInfo);


        mav.addObject("questionListdate",questionMemberdate);
        mav.addObject("datepage",pageInfodate);


        mav.addObject("memberall",member);
        mav.setViewName("QuestionTable/club_questions");

        return mav;
    }


    @RequestMapping("question/{queId}")
    public ModelAndView getDetail(@PathVariable("queId") int queId, HttpServletRequest request){
        //问
        Question question=questionService.getById(queId); //获得问题id
        System.out.println(queId);
        Member member=memberService.getbyId(question.getQueUserId()); //提问的用户对象
        //对于问题的回答
        List<Answer> answerList=answerService.getByQuestionId(queId);  //获得对这个问题的所有回答
        List<AnswerVO> answerVOList = new ArrayList<>();     //回答对象和回答用户信息
        for (Answer as:answerList){  //遍历所有回答对象
            Member m =  memberService.getbyId(as.getAnsUserId());
            AnswerVO answerVO = new AnswerVO(as,m);
            answerVOList.add(answerVO); //得到所有回答对象和用户信息
        }

        //对于回答的评论
        Map<AnswerVO,List<AncommVO>> answerVOListMap=new HashMap<>();   //评论对象和评论用户信息

        for(AnswerVO a:answerVOList){   //遍历所有的回答对象
            List<AncommVO> ancommVOList = ancommService.getByAncommId(a.getAnswer().getAnsId());
            for (AncommVO ac:ancommVOList){  //遍历所有的评论对象
                Member m = memberService.getbyId(ac.getAncomm().getAncoUserId());
                ac.setMember(m); //得到了评论对应的用户信息
            }
            answerVOListMap.put(a,ancommVOList);
        }


       QuestionVO questionVO=new QuestionVO(question,member,answerVOList,answerVOListMap);

        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        Member memberv = memberService.getbyId(userId);



        ModelAndView mv=new ModelAndView();

        mv.addObject("memberall",memberv);
        mv.addObject("questionVO",questionVO);

        mv.setViewName("QuestionDetails/question_details");


        return mv;
    }

    @PostMapping("write/answercomm")
    public ModelAndView answercomm(String ancommText,Integer questionid,Integer answerid,HttpServletRequest request){
        System.out.println("开始接受数据");
        System.out.println("开始接受数据");
        System.out.println("开始接受数据");
        System.out.println("开始接受数据");
        System.out.println("开始接受数据");

        Date date = new Date();
        System.out.println("sdfasfd--------" + ancommText);
        System.out.println("questionid------------" +questionid);
        System.out.println("answerid-------" +answerid);

        Object ss = request.getSession().getAttribute("userId");//获得session里的用户id
        int userId = Integer.parseInt(String.valueOf(ss));
        Member memberaa = memberService.getbyId(userId);

        Ancomm ancomm = new Ancomm();

        ancomm.setAncoTime(date);
        ancomm.setAncoTableId(answerid);
        ancomm.setAncoUserId(userId);
        ancomm.setAncoText(ancommText);

        System.out.println(ancomm);

         int Y = ancommMapper.insert(ancomm);

        ModelAndView mav = new ModelAndView();
        mav.addObject("memberall",memberaa);
        mav.setViewName("redirect:/question/"+questionid);

        return mav;
    }



}
